import logging
import logging.config
import model

from config import *
logging.config.fileConfig(LOGGING_CONF)
logger = logging.getLogger('dmzwatch.tasker')

def main():
     with model.session_scope() as session:
        tasks = session.query(model.Task).filter(model.Task.status.in_([ExecStatus.new, ExecStatus.running])).all()
        running = session.query(model.Task).filter_by(status = ExecStatus.running).all()
        new = session.query(model.Task).filter_by(status = ExecStatus.new).all()
        running_num = len(running)
        for task in new:
            if running_num < MAX_RUNNING_TASKS:
                running_num = running_num + 1
                task.do()
        for task in running:
            task.do()
        
            
if __name__ == '__main__':
 
    
    main()